import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-10-17
 * Time: 21:34
 */
public class test_2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int day = in.nextInt();
            int sum = 0;
            int first = in.nextInt();
            for (int i = 1; i < day; i++) {
                int x = in.nextInt();
                if (first < x) {
                    sum += (x - first);
                }
                first = x;
            }
            System.out.println(sum);
        }
    }
}
